package cn.epsilon3.mybatis.base;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import cn.epsilon3.page.Page;
import cn.epsilon3.page.PageRequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * 分页interface
 *
 * @author epsilon3
 * @date 2023/07/17
 *
 * @param <T>
 */
public interface Epsilon3SupportMapper<T> extends BaseMapper<T> {

    default void begin(PageRequestParam pageRequestParam) {
        int pageNumber = pageRequestParam.getPageNumber();
        int pageSize = pageRequestParam.getPageSize();
        if (pageNumber > 1 && pageRequestParam.isCountOptimize()) {
            PageHelper.startPage(pageNumber, pageSize, false).setReasonable(pageRequestParam.isReasonable());
        } else {
            PageHelper.startPage(pageNumber, pageSize, true).setReasonable(pageRequestParam.isReasonable());
        }

    }

    default <T> Page<T> end(List<T> list) {
        Page<T> page = new Page<>();
        PageInfo<T> pageHelperPageInfo = new PageInfo<>(list);
        page.setList(pageHelperPageInfo.getList());
        page.setPageNum(pageHelperPageInfo.getPageNum());
        page.setPages(pageHelperPageInfo.getPages());
        page.setPageSize(pageHelperPageInfo.getPageSize());
        page.setTotal(pageHelperPageInfo.getTotal());
        return page;
    }

}
